package net.wesc.ac.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ModelDialog extends DialogBox{
	private final ServiceAsync Service = GWT
		.create(Service.class);
	
	public ModelDialog(){
    	setText("Select Model Equation");
		setAnimationEnabled(false);
		setGlassEnabled(true);
		setModal(true);
		final Button closeButton = new Button("Close");
		final Button submitButton = new Button("Submit");
		VerticalPanel selectPanel = new VerticalPanel();
		selectPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		HorizontalPanel buttonPanel = new HorizontalPanel();
		
		final ListBox typeList = new ListBox();
		typeList.addItem("1-1 Electrolyte",MainPanel.E11_TYPE);
		typeList.addItem("1-2 Electrolyte",MainPanel.E12_TYPE);
		typeList.addItem("1-3 Electrolyte",MainPanel.E13_TYPE);
		typeList.addItem("2-1 Electrolyte",MainPanel.E21_TYPE);
		typeList.addItem("2-2 Electrolyte",MainPanel.E22_TYPE);
		typeList.addItem("2-3 Electrolyte",MainPanel.E23_TYPE);
		typeList.addItem("3-1 Electrolyte",MainPanel.E31_TYPE);
		typeList.addItem("3-2 Electrolyte",MainPanel.E32_TYPE);
		typeList.addItem("3-3 Electrolyte",MainPanel.E33_TYPE);
		
		final ListBox modelList = new ListBox();
		modelList.addItem("Debye Huckel Equation",MainPanel.DH_EQUATION);
		modelList.addItem("Huckel Empirical Equation",MainPanel.HE_EQUATION);
		modelList.addItem("Robinson Stokes Equation",MainPanel.RS_EQUATION);
		modelList.addItem("Guggenheim Scatchard Equation",MainPanel.GS_EQUATION);
		modelList.addItem("Electrolyte NRTL Equation",MainPanel.EN_EQUATION);
		modelList.addItem("Extended UNIQUAC Equation",MainPanel.EU_EQUATION);
		modelList.addItem("Pitzer Primitive Equation",MainPanel.PP_EQUATION);
		modelList.addItem("Pitzer Empirical Equation",MainPanel.PE_EQUATION);
		modelList.addItem("Modefied BET Equation",MainPanel.MB_EQUATION);
		
		buttonPanel.add(submitButton);
		buttonPanel.add(closeButton);
		
		selectPanel.add(typeList);
		selectPanel.add(modelList);
		selectPanel.add(buttonPanel);
		setWidget(selectPanel);
		center();
		
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				ModelDialog.this.hide();
			}
		});
		
		submitButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				ModelDialog.this.hide();
				final AlertDialog wait = new AlertDialog("Fitting,Please wait ......");
				wait.show();
								
				String data = MainPanel.dataArea.getValue();
				String temp = MainPanel.tempBox.getValue();
				if(data.matches("")){
					Window.alert("Please Input Experimental Data First!");
					wait.hide();
					return;
				}
				if(temp.matches("")){
					Window.alert("Please Input Temperature First!");
					wait.hide();
					return;
				}
				else{
					//要求温度在0K-1000K之间
					if(Double.parseDouble(temp)<0 || Double.parseDouble(temp)>1000){
						Window.alert("Temperature is invalid!");
						wait.hide();
						return;
					}
					else{
						MainPanel.model = modelList.getValue(modelList.getSelectedIndex());
						MainPanel.type = typeList.getValue(typeList.getSelectedIndex());
						Service.fit(data, MainPanel.model, MainPanel.type, temp,
							new AsyncCallback<double[]>() {

								@Override
								public void onFailure(Throwable caught) {
									// TODO Auto-generated method stub
									Window.alert("RPC ERROR:" + caught.getMessage());
								}

								@Override
								public void onSuccess(double[] result) {
									// TODO Auto-generated method stub
									if(result!=null){
										MainPanel.param = new double[result.length];
										String html = "";
										for(int i=0; i<result.length; i++){
											MainPanel.param[i] = result[i];
											html = html + "<p><font color=red>" + 'a'+i + "=" + MainPanel.param[i] + "</font></p>";
										}
										MainPanel.resultHTML.setHTML(html);
										wait.hide();
									}
									else{
										Window.alert("Model Cannot converge!");
										wait.hide();
									}
								}
							});
					}
				}
			}
		});
	}
}
